﻿using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using NaireSystem.Api;
using NaireSystem.ViewModels;
using SkiaSharp;
using System.Windows;
using System.Windows.Ink;
using TYControls;
using GalaSoft.MvvmLight;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using System.Reflection.Emit;
using Newtonsoft.Json.Linq;
using System;

namespace NaireSystem.Views
{
    /// <summary>
    /// CtrlNaireOverview.xaml 的交互逻辑
    /// </summary>
    public partial class CtrlNaireOverview : TYUserControl
    {
        ViewModelNaireOverview vm = new ViewModelNaireOverview();
        public CtrlNaireOverview()
        {
            InitializeComponent();
            this.DataContext = vm;
            this.Loaded += CtrlNaireOverview_Loaded;
        }

        private async void CtrlNaireOverview_Loaded(object sender, RoutedEventArgs e)
        {
            {
                vm.Chart2.ListSeries.Clear();
                vm.Chart2.ListXAxis.Clear();
                vm.Chart2.ListYAxis.Clear();
                var rData = await Run(() =>
                {
                    return ExamApi.GetExcellentInfo((int)(EnumExamType.Naire));
                });
                if (rData.code != 0)
                {
                    this.ShowMsg(rData.msg);
                    return;
                }
                if (rData.data != null)
                {
                    //var colSeries = new LiveChartsCore.ColumnSeries<int> { };
                    List<int> values = new List<int>();
                    List<string> Xvalues = new List<string>();
                    List<double> percent = new List<double>();
                    int maxCount = 0;
                    for (int i = 0; i < rData.data.Count && i < 3; i++)
                    {
                        values.Add(rData.data[i].count);
                        Xvalues.Add(rData.data[i].excellentUserName);
                        if (rData.data[i].count > maxCount)
                        {
                            maxCount = rData.data[i].count;
                        }
                    }
                    for (int i = values.Count; i < 3; i++)
                    {
                        values.Add(0);
                        Xvalues.Add("无");
                    }
                    int sum = values.Sum();
                    for (int i = 0; i < values.Count; i++)
                    {
                        if (sum > 0)
                        {
                            percent.Add(values[i] / sum);
                        }
                        else
                        {
                            percent.Add(0);
                        }
                    }

                    vm.Chart2.ListSeries.Add(
                    new ColumnSeries<int>
                    {
                        IsHoverable = false, // disables the series from the tooltips 
                        Values = values.ToArray(),
                        Stroke = null,
                        Fill = new SolidColorPaint(new SKColor(0x56, 0x7b, 0xf9)),
                        IgnoresBarPosition = true
                    });
                    vm.Chart2.ListYAxis = new ObservableCollection<Axis>
                    {
                    new Axis { MinLimit = 0, MaxLimit = maxCount+5 }
                    };


                    vm.Chart2.ListXAxis = new ObservableCollection<Axis>
                    {
                    new Axis
                    {
                        Labels = Xvalues.ToArray(),
                        LabelsRotation = 0,
                        SeparatorsPaint = new SolidColorPaint(new SKColor(200, 200, 200)),
                        SeparatorsAtCenter = false,
                        TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),
                        TicksAtCenter = true
                    }
                    };

                    vm.ListPie = new ObservableCollection<ISeries>();
                    for (int i = 0; i < values.Count; i++)
                    {
                        vm.ListPie.Add(new PieSeries<int>
                        {
                            Values = new List<int> { values[i] },
                            InnerRadius = 50,
                            Fill = new SolidColorPaint(new SKColor(0x56, 0x7b, 0xf9, 100)),
                            Name = Xvalues[i] + $"{(percent[i] * 100).ToString("f1")}%"
                        });
                    }
                }

            }
            {
                var rData = await Run(() =>
                {
                    return ExamApi.GetDissatisfied((int)(EnumExamType.Naire));
                });
                if (rData.code != 0)
                {
                    this.ShowMsg(rData.msg);
                    return;
                }
                vm.ListPie_Dissatisfied = new ObservableCollection<ISeries>();
                double d1 = 0, d2 = 0;
                if (rData.data.totalCount != 0)
                {
                    d1 = rData.data.dissatisfied * 1.0 / rData.data.totalCount;
                    d2 = rData.data.average * 1.0 / rData.data.totalCount;
                }
                {
                    vm.ListPie_Dissatisfied.Add(new PieSeries<double>
                    {
                        Values = new List<double> { d1 },
                        InnerRadius = 50,
                        Fill = new SolidColorPaint(new SKColor(0x18, 0xb1, 0x43)),
                        Name = $"不满意{(d1 * 100).ToString("f1")}%"
                    });
                }
                {
                    //#39A1FE
                    vm.ListPie_Dissatisfied.Add(new PieSeries<double>
                    {
                        Values = new List<double> { d2 },
                        InnerRadius = 50,
                        Fill = new SolidColorPaint(new SKColor(0x39, 0xa1, 0xfe)),
                        Name = $"一般{(d2 * 100).ToString("f1")}%"
                    });
                }
            }

            {
                var rData = await Run(() =>
                    {
                        return ExamApi.GetAveageScoreByMonth((int)(EnumExamType.Naire));
                    });
                if (rData.code != 0)
                {
                    this.ShowMsg(rData.msg);
                    return;
                }
                DateTime dt = DateTime.Now;
                List<double> values = new List<double>();
                List<int> Xvalues = new List<int>();
                for (int i = 0; i < 12; i++)
                {
                    var newDt = dt.AddMonths(i - 11);
                    var monthData = rData.data.FirstOrDefault(x => x.year == newDt.Year && x.month == newDt.Month);
                    if (monthData == null || monthData.count == 0)
                    {
                        values.Add(0);
                    }
                    else
                    {
                        values.Add(monthData.score * 1.0 / monthData.count);
                    }
                    Xvalues.Add(newDt.Month);
                }
                vm.Chart1.ListSeries.Clear();
                vm.Chart1.ListXAxis.Clear();
                vm.Chart1.ListYAxis.Clear();

                vm.Chart1.ListSeries.Add(
                    new ColumnSeries<double>
                    {
                        IsHoverable = false, // disables the series from the tooltips 
                        Values = values.ToArray(),
                        Stroke = null,
                        Fill = new SolidColorPaint(new SKColor(0x56, 0x7b, 0xf9)),
                        IgnoresBarPosition = true
                    });
                vm.Chart1.ListYAxis = new ObservableCollection<Axis>
                    {
                    new Axis { MinLimit = 0, MaxLimit = 100 }
                    };


                vm.Chart1.ListXAxis = new ObservableCollection<Axis>
                    {
                    new Axis
                    {
                        Labels = Xvalues.Select(x=>$"{x}月").ToArray(),
                        LabelsRotation = 0,
                        SeparatorsPaint = new SolidColorPaint(new SKColor(200, 200, 200)),
                        SeparatorsAtCenter = false,
                        TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),
                        TicksAtCenter = true
                    }
                    };

            }
        }
    }
}
